<template>
  <div class="list-container">
    <el-form class="search-panel" v-model="searchForm" :inline="true">
      <el-form-item label="出勤时间：">
        <el-date-picker
          v-model="searchForm.date"
          type="date"
          placeholder="选择日期"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label-width="120px" label="输入搜索：">
        <el-input v-model="searchForm.name" placeholder="劳务公司名/班组名/人员姓名"></el-input>
      </el-form-item>
      <el-form-item style="width: 100px; text-align: right">
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>
    <div class="table-panel">
      <el-row>
        <el-col :span="5" style="border-right:1px solid #e8e8e8">
          <el-tree
            :data="treeData"
            :props="defaultProps"
            @node-click="handleNodeClick"
          ></el-tree>
        </el-col>
        <el-col :span="19" style="padding-left: 10px">
          <el-table :data="tableData" width="100" show-overflow-tooltip>
            <el-table-column
              align="center"
              type="index"
              width="55"
              label="序号"
            >
            </el-table-column>
            <el-table-column align="center" prop="personName" label="姓名">
            </el-table-column>
            <!-- <el-table-column align="center" label="合计">
              <template slot-scope="scope">
                <div>{{ scope.row.totalTime }}小时</div>
              </template>
            </el-table-column> -->
            <el-table-column align="center" label="参考日工资">
              <template slot-scope="scope">
                <!-- <div>￥{{ scope.row.dailyRefTotal }}</div> -->
                <div>￥{{ scope.row.dailyWage }}</div>
              </template>
            </el-table-column>
            <el-table-column align="center"  label="参考生活费">
              <template slot-scope="scope">
                <!-- <div>￥{{ scope.row.dailyRefLive }}</div> -->
                <div>￥{{ scope.row.alimony }}</div>
              </template>
            </el-table-column>
            <el-table-column align="center"  label="实际日工资">
              <template slot-scope="scope">
                <div>￥{{ scope.row.actualDailyWage }}</div>
                <!-- <el-input size="small" v-model="scope.row.actualDailyWage">
                  <span slot="prefix">￥</span>
                </el-input> -->
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              prop="actualDailyLive"
              label="实际生活费"
            >
              <template slot-scope="scope">
                <!-- <el-input size="small" v-model="scope.row.actualAlimony">
                  <span slot="prefix">￥</span>
                </el-input> -->
                <div>￥{{ scope.row.actualAlimony }}</div>
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              label="状态"
            >
              <template slot-scope="scope">
                <!-- <el-input size="small" v-model="scope.row.actualAlimony">
                  <span slot="prefix">￥</span>
                </el-input> -->
                <div>{{ scope.row.status==0?"未确认":"已确认" }}</div>
              </template>
            </el-table-column>
            <el-table-column
              label="操作"
              align="center"
              min-width="220"
              fixed="right"
            >
              <template slot-scope="scope">
                <el-button
                :disabled="scope.row.status==1"
                  @click.native.prevent="confirmRow(scope.row)"
                  type="text"
                  icon="el-icon-circle-check"
                  >确认</el-button
                >
                <el-button
                :disabled="scope.row.status==1"
                  @click.native.prevent="checkRow(scope.row, scope.$index)"
                  type="text"
                  icon="el-icon-edit"
                  >调整</el-button
                >
                <!-- <el-button
                  @click.native.prevent="deleteRow(scope.row)"
                  type="text"
                  icon="el-icon-delete"
                  >删除</el-button
                > -->
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <el-row class="page">
        <div class="page-tag">
          共{{ Math.ceil(totalData / searchForm.pageSize) }}页/{{
            totalData
          }}条数据
        </div>
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="searchForm.currentPage"
          :page-sizes="[10, 20, 50]"
          :page-size="searchForm.pageSize"
          layout="sizes, prev, pager, next, jumper"
          :total="totalData"
        >
        </el-pagination>
      </el-row>
    </div>
    <!-- 调整弹窗 -->
    <el-dialog
      class="small-dialog"
      width="600px"
      title="调整日工资"
      :visible.sync="dialogFormVisible"
    >
      <el-form :model="dialogForm" :inline="true" label-width="100px">
        <div class="tag">参考工资</div>
        <div class="inline-form">
          <el-form-item label="参考日工资：">
            <el-input disabled v-model="dialogForm.dailyWage"></el-input>
          </el-form-item>
          <el-form-item label="参考生活费：">
            <el-input disabled v-model="dialogForm.alimony"></el-input>
          </el-form-item>
        </div>
        <div class="tag">实际工资</div>
        <div class="inline-form">
          <el-form-item label="实际日工资：">
            <el-input v-model="dialogForm.actualDailyWage"></el-input>
          </el-form-item>
          <el-form-item label="实际生活费：">
            <el-input v-model="dialogForm.actualAlimony"></el-input>
          </el-form-item>
        </div>
        <!-- <div class="tag">异议</div>
        <div class="inline-form">
          <el-form-item label="异议内容：">
            <el-input v-model="dialogForm.objection"></el-input>
          </el-form-item>
        </div> -->
        <!-- <el-form-item label="异议语音：">
          <el-input v-model="dialogForm.dailyRefTotal"></el-input>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmDialog">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getTree,
  getPersonByTeamId,
  getPersonByserviceCorporationId,
} from "@/api/worker";
import {
  dailyWageList,
  updateDailyWage,
  confirmDailyWage,
} from "@/api/wage.js";
export default {
  data() {
    return {
      searchForm: {
        name: null,
        currentPage: 1,
        pageSize: 10,
        service_corporation_id: null,
        team_id: null,
        date: null,
      },
      statusOptions: [
        { value: -1, label: "全部" },
        { value: 0, label: "未确认" },
        { value: 1, label: "已确认" },
      ],
      // currentPage: 1,
      // pageSize: 10,
      totalData: 0,
      flag: 1, //1--一级劳务公司，0--二级班组
      treeData: [],
      dialogFormVisible: false,
      dialogForm: {},
      defaultProps: {
        children: "children",
        label: "name",
      },
      tableData: [
        {
          name: "张三",
          workTypeName: "工种1",
          dailyRefTotal: "240",
          dailyRefLive: "140",
          actualDailyWage: "8,716",
          actualDailyLive: "1,130,400",
        },
        {
          name: "张三",
          workTypeName: "工种1",
          dailyRefTotal: "240",
          dailyRefLive: "140",
          actualDailyWage: "8,716",
          actualDailyLive: "1,130,400",
        },
        {
          name: "张三",
          workTypeName: "工种1",
          dailyRefTotal: "240",
          dailyRefLive: "140",
          actualDailyWage: "8,716",
          actualDailyLive: "1,130,400",
        },
        {
          name: "张三",
          workTypeName: "工种1",
          dailyRefTotal: "240",
          dailyRefLive: "140",
          actualDailyWage: "8,716",
          actualDailyLive: "1,130,400",
        },
      ],
      personId: null,
    };
  },
  created() {
    // this.getStatisticList()
    // console.log(this.$route.query.date)
    this.searchForm.date = this.$route.query.date;
    this.getTreeData();
    // this.getTableData();
    this.getDailyWageList();
  },
  methods: {
    // 获取劳务公司、班组树形数据
    getTreeData() {
      // sessionStorage.getItem("projectId")
      getTree().then((res) => {
        // console.log(res)
        this.treeData = res.data.data;
      });
    },
    // 日工资列表
    getDailyWageList() {
      dailyWageList({
        currentPage: this.searchForm.currentPage,
        pageSize: this.searchForm.pageSize,
        date: this.searchForm.date,
        name: this.searchForm.name,
        serviceCorporationId: this.searchForm.service_corporation_id,
        teamId: this.searchForm.team_id,
      }).then((res) => {
        // console.log(res);
        this.tableData = res.data.data.records;
        this.totalData = res.data.data.total;
      });
    },
    onSubmit() {
      // console.log(this.searchForm)
      this.getDailyWageList();
    },
    //   生成日工资
    //   页面大小
    handleSizeChange(val) {
      this.searchForm.pageSize = val;
      // this.getTableData();
      this.getDailyWageList();
    },
    //更改当前页
    handleCurrentChange(val) {
      this.searchForm.currentPage = val;
      // this.getTableData();
      this.getDailyWageList();
    },
    //发送请求获取表格数据
    getTableData() {
      if (this.flag === 0) {
        // 根据班组id查询班组人员————选择班组
        getPersonByTeamId(this.searchForm).then((res) => {
          console.log(res);
          this.tableData = res.data.data.records;
          this.totalData = res.data.data.total;
          // this.processTableData();
        });
      } else {
        // 根据劳务公司id查询劳务公司人员————选择劳务公司
        getPersonByserviceCorporationId(this.searchForm).then((res) => {
          console.log(res);
          this.tableData = res.data.data.records;
          this.totalData = res.data.data.total;
          // this.processTableData();
        });
      }
    },
    // 树状图
    handleNodeClick(res) {
      // console.log(res)
      if (res.children == null) {
        // console.log("班组")
        this.flag = 0;
        this.searchForm.team_id = res.id;
        this.searchForm.service_corporation_id = null;
        // this.getTableData();
        this.getDailyWageList();
      } else {
        // console.log("劳务公司")
        this.flag = 1;
        this.searchForm.service_corporation_id = res.id;
        this.searchForm.team_id = null;
        // this.getTableData();
        this.getDailyWageList();
      }
    },
    checkRow(res, index) {
      this.dialogForm = {
        actualDailyWage: res.actualDailyWage,
        actualAlimony: res.actualAlimony,
        penalty: res.penalty,
        award: res.award,
        dailyWage: res.dailyWage,
        alimony: res.alimony,
        // objection: res.objection,
      };
      this.dialogFormVisible = true;
      this.personId = res.personId;
    },
    confirmRow(res) {
      this.$confirm("是否确认" + res.personName + "的日工资?", "日工资确认", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          confirmDailyWage({
            date:this.searchForm.date,
            personId:res.personId
          }).then(e=>{
            this.$message({
              type: "success",
              message: "确认日工资成功!",
            });
            this.getDailyWageList();
          })
          
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消确认",
          });
        });
    },
    deleteRow(res) {
      this.$confirm("是否确认删除" + res.name + "的日工资?", "日工资删除", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 弹窗确认
    confirmDialog() {
      // 修改当前考勤详情
      // this.tableData[this.index].startTime = this.dialogForm.startTime;
      // this.tableData[this.index].endTime = this.dialogForm.endTime;
      // this.tableData[this.index].attendanceMode =
      //   this.dialogForm.attendanceMode;
      // this.tableData[this.index].count = this.dialogForm.count;
      updateDailyWage({
        actualAlimony: this.dialogForm.actualAlimony,
        actualDailyWage: this.dialogForm.actualDailyWage,
        date: this.searchForm.date,
        // dissentReason: this.dialogForm.objection,
        personId: this.personId,
        alimony: this.dialogForm.alimony,
        dailyWage: this.dialogForm.dailyWage,
      }).then((e) => {
        console.log(e);
        this.getDailyWageList();
        this.dialogFormVisible = false;
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.input-box {
  display: inline;
}
.steps {
  min-height: 100px;
  background-color: #ffffff;
  padding: 50px 0;
  border-bottom: solid 1px #e4e4e4;
  .el-steps {
    width: 80%;
    margin: 0 auto;
  }
  .el-step__line {
    margin-left: 100px;
  }
  .length-title {
    .el-step__line {
      margin-left: 150px;
    }
  }
  .is-success {
    border-color: #1abc9c;
    color: #1abc9c;
  }
  .el-step__main {
    margin-left: 30px;
    margin-top: -30px;
    .el-step__title {
      font-weight: bold;
    }
  }
}
</style>